package utils

import (
	"fmt"
	"html/template"

	"github.com/wneessen/go-mail"
)
const (
	htmlBodyTemplate1 = 
`  <div style="font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px;">
<div style="font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px;">
  <div
	style="max-width: 500px; margin: 0 auto; background-color: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 8px 8px rgba(0, 0, 0, 0.1);">
	<h2 style="color: #007bff; text-align: center; font-weight: 800; font-size: 30px;">Welcome to AHUT OJ!</h2>
	<p style="font-size: 16px; line-height: 1.6; margin-top: 20px;">Hi, {{.Uname}}:</p>
	<p style="font-size: 16px; line-height: 1.6; margin-bottom: 8px;">为了验证您的邮箱地址，请点击以下按钮进行验证</p>
	<div 
	  style="background-color: #1B232F; color: #eee; border-radius: 8px; padding: 10px; text-align: center; margin-top: 20px; width: 230px; margin: auto;">
	  <a style="font-size: 30px; margin: 0; letter-spacing: 4px; font-weight: 800;" href="{{.Link}}" target="_blank">前往验证</a>
	</div>
	<p style="font-size: 16px; line-height: 1.6; margin-top: 20px">点击上面按钮回跳转到验证页面以完成验证。如果您未进行相关操作，请忽略此邮件。</p>
	<span style="font-size: 16px; line-height: 1.6;">感谢您选择 AHUT OJ，Happy coding！</span>
	<p style="font-size: 16px; line-height: 1.6; text-align: center; font-size: 14px; margin-top: 12px; color: #888;">
	  Copyright © 2019 - 2024 All rights reserved.</p>
  </div>
</div>
</div>`
	htmlBodyTemplate2 = 
`  <div style="font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px;">
<div
  style="max-width: 500px; margin: 0 auto; background-color: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 8px 8px rgba(0, 0, 0, 0.1);">
  <h2 style="color: #007bff; text-align: center; font-weight: 800; font-size: 30px;">Welcome to AHUT OJ!</h2>
  <p style="font-size: 16px; line-height: 1.6; margin-top: 20px;">Hi, {{.Uname}}:</p>
  <p style="font-size: 16px; line-height: 1.6; margin-bottom: 8px;">为了验证您的邮箱地址，请使用以下验证码：</p>
  <div
	style="background-color: #1B232F; color: #eee; border-radius: 8px; padding: 10px; text-align: center; margin-top: 20px; width: 230px; margin: auto;">
	<h3 style="font-size: 30px; margin: 0; letter-spacing: 4px; font-weight: 800;">{{.Code}}</h3>
  </div>
  <p style="font-size: 16px; line-height: 1.6; margin-top: 20px">请将验证码输入到注册页面以完成验证。如果您未进行注册操作，请忽略此邮件。</p>
  <span style="font-size: 16px; line-height: 1.6;">感谢您选择 AHUT OJ，Happy coding！</span>
  <p style="font-size: 16px; line-height: 1.6; text-align: center; font-size: 14px; margin-top: 12px; color: #888;">
  	Copyright © 2019 - 2024 All rights reserved.</p>
</div>
</div>`
)
type TemplateData struct {
	Uname string
	Link string
	Code string
}
type Receiver struct {
	Email string
	Username string
}

func SendEmail(r *Receiver,sub string,data *TemplateData,htp *template.Template)error {//依次是接收者，邮件主题，邮件模板内容，邮件模板
	//邮件格式
	var emcfg=GetConfInstance().Email
	// logger.Debugf("%+v",Sdump(emcfg))
	Email := mail.NewMsg()
	if err := Email.From(emcfg.Serveremail); err != nil {
		logger.Errorf("failed to set formatted FROM address: %s", err)
		return err
	}
	if err := Email.To(r.Email); err != nil {
		logger.Errorf("failed to set To address: %s", err)
		return err
	}
	//内容
	Email.Subject(sub)
	if err := Email.SetBodyHTMLTemplate(htp,data); err != nil {
		logger.Errorf("failed to set HTML template as HTML body: %s", err)
		return err
	}
	c, err := mail.NewClient(emcfg.Stmpaddr,
		mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithSSLPort(true),
		mail.WithUsername(emcfg.Serveremail), mail.WithPassword(emcfg.Password),
	)
	if err != nil {
		logger.Errorf("failed to create client: %s", err)
		return err
	}
	defer c.Close()
	err = c.DialAndSend(Email)
	if err != nil {
		logger.Errorf("failed to deliver mail: %s", err)
		return err
	}
	return nil
}
func EmailVerifyByLink(uname string,token string,email string,path string)error{
	data:=TemplateData{
		Uname: uname,
		Link: path+"?email="+email+"&token="+token,
	}
	r:=Receiver{
		Email: email,
		Username: uname,
	}
	//邮件模板
	htp, err := template.New("htp").Parse(htmlBodyTemplate1)//使用template注入更加安全
	if err != nil {
		logger.Errorf("failed to parse text template: %s", err)
		return err
	}
	subject:=fmt.Sprintf("Hi %s ,Welcome to AhutOJ! Please confirm your email address", r.Username)
	go func(){
		err :=SendEmail(&r,subject,&data,htp)
		if err!=nil{
			logger.Errorf("failed to send email: %s", err)
		}
	}()
	return nil
}

func EmailVerifyByCode(uname string,email string,code string)error{
	data:=TemplateData{
		Uname: uname,
		Code: code,
	}
	r:=Receiver{
		Email: email,
		Username: uname,
	}
	//邮件模板
	htp, err := template.New("htp").Parse(htmlBodyTemplate2)//使用template注入更加安全
	if err != nil {
		logger.Errorf("failed to parse text template: %s", err)
		return err
	}
	subject:=fmt.Sprintf("Hi %s ,Welcome to AhutOJ! Please confirm your email address", r.Username)
	go func(){
		err :=SendEmail(&r,subject,&data,htp)
		if err!=nil{
			logger.Errorf("failed to send email: %s", err)
		}
	}()
	return nil
}